﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.Designer.Views
{
    /// <summary>
    /// PublishDataSourceToFileSystemWindow.xaml 的交互逻辑
    /// </summary>
    public partial class PublishDataSourceToFileSystemWindow : Window
    {
        private string s_targetDir;
        public PublishDataSourceToFileSystemWindow()
        {
            InitializeComponent();
        }

        private void selectDirButton_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog folderBD = new System.Windows.Forms.FolderBrowserDialog();
            if (folderBD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (folderBD.SelectedPath != string.Empty)
                {
                    s_targetDir = _ctlPublishDir.Text = string.Format("{0}\\", folderBD.SelectedPath);
                }
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            CPPEI.Coolzon.ServiceProxy.DesignServiceRef.PublishedDataSource[] dataSources = Designtime.ResPoolFactory.GetDataSourcePool().GetDataSources();
            if (dataSources != null && dataSources.Length > 0)
            {
                foreach (var item in dataSources)
                {
                    PublishDataSourceToServerModel model = new PublishDataSourceToServerModel();
                    model.Name = item.Name;
                    model.RTVersion = item.RTVersion.ToString();
                    model.PublishedVersion = item.PublishedVersion.ToString();
                    model.PublishedTime = item.PublishedTime.ToString("yyyy-MM-dd");
                    model.IsChecked = true;
                    model.Tag = item;
                    _ctlListView.Items.Add(model);
                }
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            List<ServiceProxy.DesignServiceRef.PublishedDataSource> dataSourceList = new List<ServiceProxy.DesignServiceRef.PublishedDataSource>();
            foreach (var item in _ctlListView.Items)
            {
                var model = item as PublishDataSourceToServerModel;
                if (model.IsChecked)
                {
                    dataSourceList.Add(model.Tag as ServiceProxy.DesignServiceRef.PublishedDataSource);
                }
            }
            if (dataSourceList.Count == 0)
            {
                CustomMessageBox.ShowWarning("请勾选要发布的数据源");
                return;
            }
            if (string.IsNullOrWhiteSpace(s_targetDir))
            {
                CustomMessageBox.ShowWarning("请选择发布目录");
                return;
            }
            PublishDataSourceToFileSystemThread.Start(dataSourceList, s_targetDir, Completed);
            ProgressbarWindow.Instance.ShowProgress(true);
        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }
        public void Completed(List<ServiceProxy.DesignServiceRef.PublishedDataSource> successed, List<ServiceProxy.DesignServiceRef.PublishedDataSource> failed)
        {
            string message = Environment.NewLine;
            foreach (var item in successed)
            {
                message += item.Name + "发布成功" + Environment.NewLine;
            }
            foreach (var item in failed)
            {
                message += item.Name + "发布失败" + Environment.NewLine;
            }
            Logger.InfoToTag(Library.LogTAG, "发布数据源到服务器：" + message);
            this.Dispatcher.Invoke(() =>
            {
                ProgressbarWindow.Instance.Complete();
                CustomMessageBox.ShowInfo(message);
                this.DialogResult = true;
            });
        }

        private void _cbAll_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            if (cb.IsChecked.Value)
            {
                for (int i = 0; i < _ctlListView.Items.Count; i++)
                {
                    PublishDataSourceToServerModel node = _ctlListView.Items[i] as PublishDataSourceToServerModel;
                    node.IsChecked = true;
                }
            }
            else
            {
                for (int i = 0; i < _ctlListView.Items.Count; i++)
                {
                    PublishDataSourceToServerModel node = _ctlListView.Items[i] as PublishDataSourceToServerModel;
                    node.IsChecked = false;
                }
            }
        }
    }
}
